<template>
  <el-main>
    <h3>医院设置列表</h3>
    <!-- 查询框   -->
    <el-form :inline="true" ref="ruleForm" class="demo-form-inline" :model="searchObj">
      <el-form-item prop="provinceCode">
        <el-select v-model="searchObj.provinceCode" @change="provinceChange" filterable placeholder="请选择省">
          <el-option v-for="province in provinceList" :key="province.id" :label="province.name" :value="province.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="cityCode">
        <el-select v-model="searchObj.cityCode" @change="cityChange" filterable placeholder="请选择市">
          <el-option v-for="city in cityList" :key="city.id" :label="city.name" :value="city.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="districtCode">
        <el-select v-model="searchObj.districtCode" filterable placeholder="请选择区">
          <el-option v-for="district in districtList" :key="district.id" :label="district.name" :value="district.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="hosname">
        <el-input v-model="searchObj.hosname" placeholder="医院名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getHospitalList()" icon="el-icon-search">查询</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 展示列表   -->
    <el-table v-loading="listLoading" :data="hospList" stripe border >
      <el-table-column label="序号" width="90" align="center">
        <template slot-scope="scope">
          {{ (current-1)*limit+scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="医院 logo" align="center">
        <template slot-scope="scope">
          <img :src="'data:image/jpeg;base64,'+scope.row.logoData" width="60" :alt="scope.row.hosname+'医院logo'"/>
        </template>
      </el-table-column>
      <el-table-column prop="hosname" label="医院名称" width="320" align="center"></el-table-column>
      <el-table-column prop="param.hostypeStr" label="医院等级" width="90" align="center"></el-table-column>
      <el-table-column prop="param.fullAddress" label="详细地址" align="center"></el-table-column>
      <el-table-column label="状态" width="70" align="center">
        <template slot-scope="scope">
          <span style="color:green" v-if="scope.row.status === 1">已上线</span>
          <span style="color:red" v-if="scope.row.status === 0">未上线</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
      <el-table-column fixed="right" label="操作" width="280" align="center">
        <template slot-scope="scope">
          <router-link :to="'/hospital/show/'+scope.row.id">
            <el-button type="primary" icon="el-icon-search" size="small">查看</el-button>
          </router-link>
          <router-link :to="'/hospital/schedule/'+scope.row.hoscode">
            <el-button type="primary" icon="el-icon-search" size="small">排班</el-button>
          </router-link>
          <el-button type="warning" icon="el-icon-lock" size="small" v-if="scope.row.status === 1" @click="updateHospStatus(scope.row.id, 0, scope.row.hosname)">下线</el-button>
          <el-button type="success" icon="el-icon-unlock" size="small" v-if="scope.row.status === 0" @click="updateHospStatus(scope.row.id, 1, scope.row.hosname)">上线</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @current-change="getHospitalList"
      @size-change="changeSize"
      background
      :current-page="current"
      :page-size="limit"
      :page-sizes="[5,10,20,30,40,50,100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="padding-top: 27px;">
    </el-pagination>
  </el-main>
</template>

<script>
import dict from '../../api/cmn/dict'
import hospital from '../../api/hospital'

export default {
  name: 'list',
  data() {
    return {
      provinceList: [],// 省份列表
      cityList: [],// 城市列表
      districtList: [],// 区级列表
      searchObj: {},// 查询条件
      hospList: [],// 医院列表
      listLoading: true,// 数据是否正在加载
      total:0,// 数据库中的总记录数
      current: 1, // 当前页
      limit: 10,// 每页记录数
    }
  },
  mounted() {
    // 查询医院信息
    this.getHospitalList();
    // 获取所有省份信息
    this.getProvinceList();
  },
  methods: {
    // 1、查询医院信息
    getHospitalList(page=1) {
      this.current = page;
      hospital.getPageList(this.current, this.limit, this.searchObj).then(
        response => {
          // 医院列表
          this.hospList = response.data.content;
          // 总记录数
          this.total = response.data.totalPages;
          this.listLoading = false;
        }
      ).catch(
        error => {
          this.$message({
            type: 'error',
            message: '数据请求失败!'+error
          });
        }
      );
    },

    // 2、获取所有省份信息
    getProvinceList() {
      dict.findByDictCode("Province").then(
        response => {
          this.provinceList = response.data;
        }
      ).catch(
        error => {
          this.$message({
            type: 'error',
            message: '数据请求失败!'+error
          });
        }
      );
    },

    // 3、省份选择改变后
    provinceChange() {
      // 通过省份 id 获取城市列表
      dict.findByParentId(this.searchObj.provinceCode).then(
        response => {
          this.cityList = response.data;
        }
      ).catch(
        error => {
          this.$message({
            type: 'error',
            message: '数据请求失败!'
          });
        }
      )
    },

    // 4、城市选择改变后
    cityChange() {
      // 通过城市 id 获取区列表
      dict.findByParentId(this.searchObj.cityCode).then(
        response => {
          this.districtList = response.data;
        }
      ).catch(
        error => {
          this.$message({
            type: 'error',
            message: '数据请求失败!'
          });
        }
      )
    },

    // 5、重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      // 清空数据
      this.cityList = [];
      this.districtList = [];
    },

    // 6、改变一页显示的数据数
    changeSize(size) {
      this.limit = size;
      this.getHospitalList(1);
    },

    // 7、更新医院的上线状态
    updateHospStatus(id, status, hosname) {
      hospital.updateHospStatus(id, status).then(
        response => {
          // 提示
          this.$message({
            type: 'success',
            message: '修改成功, '+hosname+(status === 1 ? ' 已上线!' : ' 已下线!')
          });
          // 刷新列表
          this.getHospitalList(this.current);
        }
      ).catch(
        error => {
          // 提示
          this.$message({
            type: 'error',
            message: '修改失败!'
          });
        }
      )
    }
  }
}
</script>

<style scoped>

</style>
